# coding: utf-8
from PySide6.QtCore import Qt
from PySide6.QtWidgets import QWidget
from qfluentwidgets import SingleDirectionScrollArea, setFont, ExpandLayout, FluentIcon as FIF, InfoBar, BodyLabel, \
    SettingCardGroup, SwitchSettingCard, OptionsSettingCard, CustomColorSettingCard, setThemeColor, setTheme, \
    HyperlinkCard, PrimaryPushSettingCard, ColorSettingCard, RangeSettingCard

from common import isWin11, cfg, signalBus, HELP_LINK, APP_NAME, APP_VERSION, AUTHOR, YEAR, FEEDBACK_URL, RELEASE_LINK, \
    openUrl
from components import WallpaperCard


class SettingInterface(SingleDirectionScrollArea):

    def __init__(self, parent=None):
        super().__init__(parent)
        self.scrollWidget = QWidget()
        self.expandLayout = ExpandLayout(self.scrollWidget)

        self.titleLabel = BodyLabel('设置', self.scrollWidget)
        self.personalGroup = SettingCardGroup(self.tr('个性化'), self.scrollWidget)
        self.micaCard = SwitchSettingCard(
            FIF.TRANSPARENT,
            self.tr('云母效果'),
            '',
            cfg.micaEnabled,
            self.personalGroup
        )
        self.themeCard = OptionsSettingCard(
            cfg.themeMode,
            FIF.BRUSH,
            self.tr('应用程序的主题'),
            '',
            texts=[
                self.tr('明亮'), self.tr('黑暗'),
                self.tr('系统默认')
            ],
            parent=self.personalGroup
        )
        self.themeColorCard = CustomColorSettingCard(
            cfg.themeColor,
            FIF.PALETTE,
            self.tr('主题颜色'),
            '',
            self.personalGroup
        )
        self.zoomCard = OptionsSettingCard(
            cfg.dpiScale,
            FIF.ZOOM,
            self.tr("界面缩放"),
            '',
            texts=[
                "100%", "125%", "150%", "175%", "200%",
                self.tr("系统默认")
            ],
            parent=self.personalGroup
        )
        # 壁纸
        self.wallpaperGroup = SettingCardGroup(self.tr('壁纸'), self.scrollWidget)
        self.wallpaperEnabledCard = SwitchSettingCard(
            FIF.PLAY_SOLID,
            self.tr('壁纸'),
            '',
            cfg.wallpaperEnabled,
            self.wallpaperGroup
        )
        self.wallpaperCard = WallpaperCard(
            cfg.wallpaperImage,
            FIF.PHOTO,
            '选择壁纸',
            '',
            self.wallpaperGroup
        )
        self.wallpaperLightColorCard = ColorSettingCard(
            cfg.wallpaperLightColor,
            FIF.PALETTE,
            self.tr('明亮背景颜色'),
            '',
            self.wallpaperGroup
        )
        self.wallpaperDarkColorCard = ColorSettingCard(
            cfg.wallpaperDarkColor,
            FIF.PALETTE,
            self.tr('黑暗背景颜色'),
            '',
            self.wallpaperGroup
        )
        self.wallpaperOpacityCard = RangeSettingCard(
            cfg.wallpaperOpacity,
            FIF.BRIGHTNESS,
            self.tr('壁纸蒙版透明度'),
            '',
            self.wallpaperGroup
        )

        # 应用程序
        self.aboutGroup = SettingCardGroup(self.tr('关于'), self.scrollWidget)
        self.helpCard = HyperlinkCard(
            HELP_LINK,
            self.tr('打开帮助页面'),
            FIF.HELP,
            self.tr('帮助'),
            '',
            self.aboutGroup
        )
        self.feedbackCard = PrimaryPushSettingCard(
            self.tr('提供反馈'),
            FIF.FEEDBACK,
            self.tr('提供反馈'),
            self.tr(f'通过提供反馈帮助我们改进 {APP_NAME}'),
            self.aboutGroup
        )
        self.aboutCard = PrimaryPushSettingCard(
            self.tr('检查更新'),
            FIF.INFO,
            self.tr('关于'),
            '© ' + self.tr('版权') + f" {YEAR}, {AUTHOR}. " + self.tr('Version') + " " + APP_VERSION,
            self.aboutGroup
        )

        self.__initWidgets()

    def __initWidgets(self):
        self.setObjectName("settingInterface")
        self.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
        self.setWidget(self.scrollWidget)
        self.setWidgetResizable(True)
        self.scrollWidget.setAutoFillBackground(False)
        self.enableTransparentBackground()

        setFont(self.titleLabel, 33)
        self.titleLabel.setFixedHeight(80)
        self.micaCard.setEnabled(isWin11())

        self.__initLayout()
        self.__connectSignalToSlot()

    def __initLayout(self):
        self.expandLayout.setContentsMargins(0, 0, 10, 10)
        self.expandLayout.addWidget(self.titleLabel)

        self.personalGroup.addSettingCard(self.micaCard)
        self.personalGroup.addSettingCard(self.themeCard)
        self.personalGroup.addSettingCard(self.themeColorCard)
        self.personalGroup.addSettingCard(self.zoomCard)

        self.wallpaperGroup.addSettingCard(self.wallpaperEnabledCard)
        self.wallpaperGroup.addSettingCard(self.wallpaperCard)
        self.wallpaperGroup.addSettingCard(self.wallpaperLightColorCard)
        self.wallpaperGroup.addSettingCard(self.wallpaperDarkColorCard)
        self.wallpaperGroup.addSettingCard(self.wallpaperOpacityCard)

        self.aboutGroup.addSettingCard(self.helpCard)
        self.aboutGroup.addSettingCard(self.feedbackCard)
        self.aboutGroup.addSettingCard(self.aboutCard)

        self.expandLayout.addWidget(self.personalGroup)
        self.expandLayout.addWidget(self.wallpaperGroup)
        self.expandLayout.addWidget(self.aboutGroup)

    def __connectSignalToSlot(self):
        cfg.themeChanged.connect(setTheme)
        cfg.appRestartSig.connect(self.__onAppRester)
        self.themeColorCard.colorChanged.connect(lambda c: setThemeColor(c))
        self.micaCard.checkedChanged.connect(self.window().setMicaEffectEnabled)
        self.wallpaperEnabledCard.checkedChanged.connect(signalBus.wallpaperEnabledChanged)
        self.wallpaperCard.fileChanged.connect(signalBus.wallpaperChanged)
        self.wallpaperOpacityCard.valueChanged.connect(signalBus.wallpaperOpacityChanged)
        self.wallpaperLightColorCard.colorChanged.connect(signalBus.wallpaperLightColorChanged)
        self.wallpaperDarkColorCard.colorChanged.connect(signalBus.wallpaperDarkColorChanged)

        self.feedbackCard.clicked.connect(lambda: openUrl(FEEDBACK_URL))
        self.aboutCard.clicked.connect(lambda: openUrl(RELEASE_LINK))

    def __onAppRester(self):
        InfoBar.warning('', '重启后生效', duration=2000, parent=self)
